// 20210107 判断一个内容是否为空，是否有值
export const isNotAvailable = (input_value) => {
    return input_value === undefined || input_value === null || isNaN(input_value);
};

// 20210108 To Judge whether a obj is a String
export const isString = (text) => {
  if (text instanceof String) return true;
  else if (typeof text === "string") return true;
  return false;
};

// 20210108 To Judge whether a obj is a Array
export const isArray = (obj) => {
   return obj instanceof Array;
};

// 20210108 To transform a string with px to a number.
const transformFromPxToNum = (text) => {
    if (isString(text)) {
        let length = text.length;
        return parseFloat(text.substring(0, length - 2));
    } else {
        return 0;
    }
};

// 20210108 Get Absolute Element's position
export const getAbsoluteElementPosition = (elem) => {
    return {
        x: transformFromPxToNum(elem.style.left),
        y: transformFromPxToNum(elem.style.top),
    }
};

// 210110 Create Element By Configuration
export const generateElement = ({
                                    type,
                                    width,
                                    height,
                                    backgroundColor,
                                    border,
                                    margin,
                                    padding,
                                    position,
                                    display,
                                    left,
                                    top,
                                    innerText,
                                    overflow,
                                    color,
                                    lineHeight,
                                    className,
                                    fontSize,
                                    marginLeft,
                                }) => {
    if (type) {
        let elem = document.createElement(type);
        let elemStyle = elem.style;
        if (width) elemStyle.width = width;
        if (height) elemStyle.height = height;
        if (backgroundColor) elemStyle.backgroundColor = backgroundColor;
        if (border) elemStyle.border = border;
        if (margin) elemStyle.margin = margin;
        if (padding) elemStyle.padding = padding;
        if (position) elemStyle.position = position;
        if (display) elemStyle.display = display;
        if (left) elemStyle.left = left;
        if (top) elemStyle.top = top;
        if (innerText) elem.innerText = innerText;
        if (overflow) elem.style.overflow = overflow;
        if (color) elem.style.color = color;
        if (lineHeight) elem.style.lineHeight = lineHeight;
        if (className) elem.className = className;
        if (fontSize) elem.style.fontSize = fontSize;
        if (marginLeft) elem.style.marginLeft = marginLeft;
        return elem;
    }
};

// Convert decimal to hexadecimal
export const trans10to16 = (value10) =>{
    if (typeof value10 === "number"){
        let baseStr = "";
        let numStr = value10.toString(16);
        for (let i = 0;i< 4 - numStr.length; i++){
            baseStr += "0";
        }
        return baseStr + numStr.toUpperCase() + "H";
    }
    return null;
};
